package com.caird.pmp.iot.message.impl;

import com.caird.pmp.iot.message.MessageBase;
import io.netty.buffer.ByteBuf;
import lombok.Data;

@Data
public class DeviceMessage0100 extends MessageBase {

    public DeviceMessage0100() {
        setId((short)0x0100);
    }

    @Override
    public boolean decodeBody(ByteBuf raw) {
        if (getBodyLength() < 36)
            return false;

        raw.skipBytes(4);

        //制造商ID
        byte[] bs = new byte[5];
        raw.readBytes(bs);
        maker = new String(bs).trim();

        //终端型号
        bs = new byte[20];
        raw.readBytes(bs);
        model = new String(bs).trim();

        //终端ID
        bs = new byte[7];
        raw.readBytes(bs);
        sn = new String(bs).trim();

        return true;
    }

    @Override
    public boolean encodeBody(ByteBuf raw) {
        if (raw.writableBytes() < 40)
            return false;

        //省域ID + 市县域ID
        raw.writeZero(4);

        //maker
        String val = maker;
        if (null == val)
            val = "";
        if (val.length() > 5)
            val = val.substring(0, 5);
        raw.writeBytes(val.getBytes());
        raw.writeZero(5 - val.length());

        //model
        val = model;
        if (null == val)
            val = "";
        if (val.length() > 20)
            val = val.substring(0, 20);
        raw.writeBytes(val.getBytes());
        raw.writeZero(20 - val.length());

        //sn
        val = sn;
        if (null == val)
            val = "";
        if (val.length() > 7)
            val = val.substring(0, 7);
        raw.writeBytes(val.getBytes());
        raw.writeZero(7 - val.length());

        //车牌颜色 + 车辆标识
        raw.writeZero(1);

        return true;
    }

    //制造商ID
    private String maker;

    //终端型号
    private String model;

    //终端ID
    private String sn;

}
